//
//  NewsViewController.m
//  OurRelax
//
//  Created by 于宾尧 on 15/6/16.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//

#import "NewsViewController.h"

#import "Player.h"

@interface NewsViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation NewsViewController

#warning 1 问题根源
//- (void)viewWillAppear:(BOOL)animated
//{
//    self.view.frame = [[UIScreen mainScreen]bounds];
//    self.mytableview.frame =CGRectMake(self.view.frame.origin.x,65, self.view.frame.size.width, self.view.frame.size.height  - 65- 44 );
//}

- (void)viewDidLoad {
    [super viewDidLoad]; 
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
#warning 3
    self.page = 2;
    self.NewsArray = [NSMutableArray array];
    
    self.mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 69) style:UITableViewStylePlain];
    
    self.mytableview.dataSource = self;
    self.mytableview.delegate = self;
    [self.mytableview       setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self.view addSubview:self.mytableview];
    
    
//    [self footerRefesh];
    
    [self footerRefesh];
    [self AFN];
    
    [self shangla];
    
}

#warning 2
- (void)shangla
{
    self.mytableview.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self.NewsArray removeAllObjects];
        
        [self AFN];
        
        [self.mytableview reloadData];
        
        [self.mytableview.header endRefreshing];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    self.nnn  =  [[NewsTableViewCell alloc]init];
    NewsModel *model = [self.NewsArray objectAtIndex:indexPath.row];
    
    //    NSURL *url = [NSURL fileURLWithPath:filePath];
    //    NSString *asd = (NSString *) self.shipin.count;
    
    
    NSURL *url = [NSURL URLWithString:model.video_link];
    //    NSLog(@"*********************%@",model.video_link);
    // 使用本地url资源初始化播放器
    self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    
    // MPMovieScalingModeAspectFit 视频按比例填在View上，保持宽高比
    // MPMovieScalingModeFill 视频不考虑高宽比而是单独针对水平轴和垂直轴进行缩放
    [_player.moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
    [_player.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
    [self.view addSubview:_player.view];
    [_player.view setFrame:CGRectMake(50, 50, 220, 220)];
    //    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(M_PI/2);
    //    _player.view.transform = landscapeTransform;
    
    [self presentMoviePlayerViewControllerAnimated:_player];
#pragma 改
    [[Player shareDataHandle].player stop];
    [TagHandle shareTagHandle].isPlaying = ![TagHandle shareTagHandle].isPlaying;
    
    [_player.moviePlayer play];//播放
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
}
- (void)doFinished:(id)sender
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [_player.view removeFromSuperview];
    
    
#warning 4
    //[self. mytableview reloadData];
    
    _player = nil;// 释放资源
}
- (void)AFN
{
    self.NewsArray = [NSMutableArray array];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://rong.36kr.com/api/v1/news?category=tv&page=1&per_page=10"];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSDictionary *dataDic = responseObject;
        
        NSMutableArray *array = [NSMutableArray array];
        array = [[dataDic objectForKey:@"data"]objectForKey:@"feeds"];
        
        for (NSMutableDictionary *dic in array) {
            NewsModel *newa = [[NewsModel alloc]init];
            [newa setValuesForKeysWithDictionary:dic];
            
            self.news = newa;
            
            self.asddd = newa.feed_id;
            NSLog(@"**********************%@",self.asddd);
            //            NSLog(@"*/*/*/*/*/*/*/*/*/*/*/**//*/*%@", newa.video_link);
            [self.shipin addObject:newa.video_link];
            [self.NewsArray addObject:newa];
            
            
            [self.mytableview reloadData];
        }
        
        
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        
        NSLog(@"活动请求失败==== %@",error);
        
    }];
    
    
    
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.NewsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"NewsReuse";
    
    NewsTableViewCell *cell  =  [tableView dequeueReusableCellWithIdentifier:reuse];
    
    if (!cell) {
        cell = [[NewsTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    
    cell.news = [self.NewsArray objectAtIndex:indexPath.row];
    
    return cell;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
    
}

-(void)footerRefesh
{

    self.mytableview.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        self.page++;
    
        [self getAfnData];
        
        [self.mytableview reloadData];
        
        
        
    }];
    
    
    
    
}

- (void)getAfnData
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    

     NSString *url_string = [NSString stringWithFormat:@"http://rong.36kr.com/api/v1/news?category=tv&last_id=%@&page=%ld&per_page=10&request_type=1", self.asddd, self.page];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSDictionary *dataDic = responseObject;
        
        NSMutableArray *array = [NSMutableArray array];
        array = [[dataDic objectForKey:@"data"]objectForKey:@"feeds"];
        
        for (NSMutableDictionary *dic in array) {
            NewsModel *newa = [[NewsModel alloc]init];
            [newa setValuesForKeysWithDictionary:dic];
            
            
            
            self.asddd = newa.feed_id;
            //NSLog(@"**********************%@",self.asddd);
            //            NSLog(@"*/*/*/*/*/*/*/*/*/*/*/**//*/*%@", newa.video_link);
            [self. shipin addObject:newa.video_link];
            [self. NewsArray addObject:newa];
            
            
            [self. mytableview reloadData];
        }
        
        
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        
        NSLog(@"活动请求失败==== %@",error);
        
    }];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
